package com.dinadi.yidi.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.MainPagerAdapter;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.base.BaseFragment;
import com.dinadi.yidi.ui.fragment.MyCouponsStateFragment;
import com.dinadi.yidi.ui.fragment.MyCouponsStateFragment2;
import com.dinadi.yidi.ui.fragment.MyCouponsStateFragment3;
import com.dinadi.yidi.utils.TLog;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/9/18  我的优惠券
 * Time: 11:28
 */
public class MyCouponsActivity extends BaseActivity {
    private View view;
    private TextView tvTitle, text_title_right;
    private ImageView img_coupon_center;
    private ViewPager id_activity_main_vp;
    private RadioButton rb_available, rb_already_in_use, rb_be_overdue;
    private MainPagerAdapter mAdapter;
    private List<BaseFragment> fragments = new ArrayList<>();
    private List<RadioButton> radioButtons = new ArrayList<>();


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(MyCouponsActivity.this).inflate(R.layout.activity_coupons, null);
        return view;
    }

    @Override
    protected void initView() {
        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        tvTitle.setText(getResources().getString(R.string.text_my_coupons));
        text_title_right = (TextView) view.findViewById(R.id.text_title_right);
        text_title_right.setText(getResources().getString(R.string.text_my_exchange));
        id_activity_main_vp = (ViewPager) view.findViewById(R.id.id_activity_main_vp);
        rb_available = (RadioButton) view.findViewById(R.id.rb_available);
        rb_already_in_use = (RadioButton) view.findViewById(R.id.rb_already_in_use);
        rb_be_overdue = (RadioButton) view.findViewById(R.id.rb_be_overdue);
        img_coupon_center = (ImageView) view.findViewById(R.id.img_coupon_center);
    }

    @Override
    protected void initListener() {

        registerClickListener(text_title_right, img_coupon_center);

        rb_available.setOnClickListener(this);
        rb_already_in_use.setOnClickListener(this);
        rb_be_overdue.setOnClickListener(this);
        id_activity_main_vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectRadioButton(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.rb_available:
                id_activity_main_vp.setCurrentItem(0);
                break;
            case R.id.rb_already_in_use:
                id_activity_main_vp.setCurrentItem(1);
                break;
            case R.id.rb_be_overdue:
                id_activity_main_vp.setCurrentItem(2);
                break;
            case R.id.text_title_right:
                /*兑换码*/
                Intent intent = new Intent(MyCouponsActivity.this, ExChangeActivity.class);
                startActivity(intent);
                break;
            case R.id.img_coupon_center:
                /*进入领券中心*/
                Intent couponIntent = new Intent(MyCouponsActivity.this, CouponCenterActivity.class);
                startActivity(couponIntent);
                break;
            default:
                break;

        }
    }

    @Override
    protected void initData() {
        TLog.error("fragment2的initData（）");
        fragments.add(new MyCouponsStateFragment());//可用
        fragments.add(new MyCouponsStateFragment2());//已使用
        fragments.add(new MyCouponsStateFragment3());//过期
        radioButtons.add(rb_available);
        radioButtons.add(rb_already_in_use);
        radioButtons.add(rb_be_overdue);

        selectRadioButton(0);
        id_activity_main_vp.setCurrentItem(0);

        if (mAdapter == null) {
            mAdapter = new MainPagerAdapter(MyCouponsActivity.this.getSupportFragmentManager(), fragments);
            id_activity_main_vp.setAdapter(mAdapter);
        }

    }

    /**
     * 设置当前选中的radiobutton的文字颜色
     *
     * @param i
     */
    private void selectRadioButton(int i) {
        for (int j = 0; j < 3; j++) {
            if (j == i) {
                radioButtons.get(j).setTextColor(getResources().getColor(R.color.sjs_text));
                radioButtons.get(j).setChecked(true);
            } else {
                radioButtons.get(j).setTextColor(getResources().getColor(R.color.home_text));
            }
        }

    }
}
